package com.turing.post.report.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.AccSysDaySett;
import com.turing.post.report.util.AccSysDaySettDao;
import com.turing.post.util.Config;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "report.impl.AccSysDaySettDaoImpl")
@Remote
@Local
public class AccSysDaySettDaoImpl implements AccSysDaySettDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	public void generateAccSysDaySett(String issueCompanyNo, Date tranDate) {
		Query query = _em
				.createNativeQuery("insert into AccSysDaySett ( IssueCompanyNo, TranDate, "
						+ "LastBalance, SaleAmount, DepositAmount, ConsumeAmount, RedemingAmount, FeeAmount, AdjustAmount, Balance ) "
						+ "select t.issueCompanyNo, t.tranDate, "
						+ "sum(t.lastBalance), sum(t.saleAmount), sum(t.depositAmount), sum(t.consumeAmount), sum(t.redemingAmount), sum(t.feeAmount), sum(t.adjustAmount), sum(t.balance) "
						+ "from AccSysBranchDaySett t "
						+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2 "
						+ "group by t.issueCompanyNo, t.tranDate");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}

	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeAllAccSysDaySettByTranDate(String issueCompanyNo,
			Date tranDate) {
		Query query = _em.createNativeQuery("delete from AccSysDaySett t "
				+ "where t.issueCompanyNo = ?1 and t.tranDate = ?2");
		query.setParameter(1, issueCompanyNo);
		query.setParameter(2, tranDate);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccSysDaySett> getAccSysDaySettList(PCPSWebInput webInput,
			Integer page, String startDate, String endDate) {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em
				.createNativeQuery(
						"Select t.* from AccSysDaySett t "
								+ "where t.issueCompanyNo = ?1 "
								+ "and t.tranDate>=to_date(?2,'yyyy-MM-dd hh24:mi:ss') "
								+ "and t.tranDate<=to_date(?3,'yyyy-MM-dd hh24:mi:ss') "
								+ "order by t.issueCompanyNo, t.tranDate ",
						AccSysDaySett.class).setFirstResult(startPosition)
				.setMaxResults(maxResult);
		query.setParameter(1, issueCompanyNo);
		if (!PCPSUtil.isEmpty(startDate)) {
			query.setParameter(2, startDate + " 00:00:00");
		} else {
			query.setParameter(2, PCPSUtil.getPackedYMDString2(new Date())
					+ " 00:00:00");
		}
		if (!PCPSUtil.isEmpty(endDate)) {
			query.setParameter(3, endDate + " 23:59:59");
		} else {
			query.setParameter(3, PCPSUtil.getPackedYMDString2(new Date())
					+ " 23:59:59");
		}
		return query.getResultList();
	}
}
